#coding=utf-8

import re
import os

tmp_head_view = """<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>"""

tmp_form_head = """        <record id="{}" model="ir.ui.view">
            <field name="name">{}</field>
            <field name="model">{}</field>
            <field name="arch" type="xml">
                <form string="{}">
                    <group>"""

tmp_field_view ="""                        <field name="{}" />"""

tmp_form_last="""                    </group>
                </form>
            </field>
        </record>"""

tmp_tree_head = """        <record id="{}" model="ir.ui.view">
            <field name="name">{}</field>
            <field name="model">{}</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="{}" >"""

tmp_tree_last = """                </tree>
            </field>
        </record>"""

tmp_search_head = """        <record id="{}" model="ir.ui.view">
            <field name="name">{}</field>
            <field name="model">{}</field>
            <field name="arch" type="xml">
                <search string="Search Payslips">"""

tmp_search_last = """                </search>
            </field>
        </record>"""

tmp_act_view = """        <record id="{}" model="ir.actions.act_window">
            <field name="name">{}</field>
            <field name="res_model">{}</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="{}"/>
            <field name="search_view_id" ref="{}"/>
        </record>"""

tmp_menu_view = """        <menuitem id="{}" name="{}" sequence="10" action="{}" parent="{}"/>"""

tmp_last_view = """    </data>
</openerp>"""



name_re = re.compile(r"\'([^\']*)\'")
name_line_re = re.compile(r"    _name")
field_re = re.compile(r"fields\.")

def write_view_from_file(file_name,target_file):
    fwrite = open(target_file, 'w')
    fwrite.write(tmp_head_view)
    fwrite.write("\n")
    fwrite.close()

    if os.path.exists(file_name):
        if os.path.isfile(file_name):
            write_view_config(file_name,target_file)




def write_view_config(file_name,target_file):
    fwrite = open(target_file, 'a')
    with open(file_name, 'r') as f:
        line = 0
        line_list = []
        for line in f.readlines():
            if name_line_re.findall(line):
                print line
                names = name_re.findall(line.strip())
                name = names[0]
                name_normal = name.replace('.','_')
                name_form_id = name_normal+"_form"
                name_tree_id = name_normal+"_tree"
                name_filter_id = name_normal+"_filter"
                name_act_id = "act_"+name_normal
                name_menu_id = "menu_"+name_normal
                fwrite.write(tmp_form_head.format(name_form_id,name_form_id,name,name_form_id))
                fwrite.write("\n")
                fwrite.write(tmp_form_last)
                fwrite.write("\n")
                fwrite.write("\n")

                fwrite.write(tmp_tree_head.format(name_tree_id,name_tree_id,name,name_tree_id))
                fwrite.write("\n")
                fwrite.write(tmp_tree_last)
                fwrite.write("\n")
                fwrite.write("\n")

                fwrite.write(tmp_search_head.format(name_filter_id,name_filter_id,name))
                fwrite.write("\n")
                fwrite.write(tmp_search_last)
                fwrite.write("\n")
                fwrite.write("\n")

                fwrite.write(tmp_act_view.format(name_act_id," ",name,name_tree_id,name_filter_id))
                fwrite.write("\n")
                fwrite.write("\n")

                fwrite.write(tmp_menu_view.format(name_menu_id," ",name_act_id," "))
                fwrite.write("\n")
                fwrite.write("\n")
    fwrite.write(tmp_last_view)
    fwrite.close()




    fwrite.close()

if __name__ =='__main__':

    file_names = [
        'patient_daily_sleep_report_config.py'
    ]
    for file_name in file_names:
        if file_name.find(".py")>0:
            new_file_name = file_name.split(".")[0]+"_view_tmp.xml"
            write_view_from_file(file_name,new_file_name)
        else:
            print "file: {} is not a valid file".format(file_name)





